package com.bingxue.edu.teacher.service;

import com.bingxue.edu.common.enums.LessonStatusEnum;
import com.bingxue.edu.framework.common.model.BaseEntity;
import com.bingxue.edu.management.education.model.entity.Lesson;
import com.bingxue.edu.management.education.service.LessonService;
import com.bingxue.edu.management.education.service.LessonStudentService;
import com.bingxue.edu.management.report.model.resp.LessonSignInStat;
import com.bingxue.edu.teacher.model.query.TeacherLessonStatQuery;
import com.bingxue.edu.teacher.model.query.TeacherScheduleLessonQuery;
import com.bingxue.edu.teacher.model.resp.TeacherScheduleLessonResp;
import com.bingxue.edu.teacher.model.resp.TeacherScheduleStatResp;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.bingxue.edu.management.education.model.entity.table.ClassInfoTableDef.CLASS_INFO;
import static com.bingxue.edu.management.education.model.entity.table.LessonTableDef.LESSON;

@Slf4j
@Service
@RequiredArgsConstructor
public class TeacherScheduleService {

    private final LessonService lessonService;
    private final LessonStudentService lessonStudentService;

    public List<TeacherScheduleLessonResp> getScheduleLessonList(TeacherScheduleLessonQuery query) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(
                        LESSON.DEFAULT_COLUMNS,
                        CLASS_INFO.DEFAULT_COLUMNS
                )
                .from(LESSON)
                .leftJoin(CLASS_INFO).on(LESSON.CLASS_ID.eq(CLASS_INFO.ID))
                .where(LESSON.LEAD_TEACHER_ID.eq(query.getLeadTeacherId()))
                    .and(LESSON.ASSIST_TEACHER_ID.eq(query.getAssistTeacherId()))
                    .and(LESSON.LESSON_DATE.ge(query.getLessonDateFrom()))
                    .and(LESSON.LESSON_DATE.lt(query.getLessonDateTo()));
//                .orderBy(LESSON.START_TIME,true);
        List<TeacherScheduleLessonResp> resp = lessonService.getMapper().selectListWithRelationsByQueryAs(queryWrapper, TeacherScheduleLessonResp.class);
        Map<String, TeacherScheduleLessonResp> scheduleLessonMap = resp.stream().collect(Collectors.toMap(BaseEntity::getId, item -> item));
        List<LessonSignInStat> lessonSignInStatList = lessonStudentService.getLessonSignInStat(scheduleLessonMap.keySet());
        for (LessonSignInStat lessonSignInStat : lessonSignInStatList) {
            TeacherScheduleLessonResp item = scheduleLessonMap.get(lessonSignInStat.getLessonId());
            if(item != null) {
                item.setTotalCount(lessonSignInStat.getTotalCount());
                item.setSignInCount(lessonSignInStat.getSignInCount());
            }
        }
        return resp;
    }

    public TeacherScheduleStatResp getScheduleLessonStat(TeacherLessonStatQuery query) {
        TeacherScheduleStatResp teacherScheduleStatResp = new TeacherScheduleStatResp();
        // 查询教师时间段内的排课情况
        List<Lesson> lessons = lessonService.queryChain()
                .where(LESSON.LEAD_TEACHER_ID.eq(query.getLeadTeacherId()))
                .and(LESSON.ASSIST_TEACHER_ID.eq(query.getAssistTeacherId()))
                .and(LESSON.LESSON_DATE.ge(query.getLessonDateFrom()))
                .and(LESSON.LESSON_DATE.le(query.getLessonDateTo()))
                .list();
        teacherScheduleStatResp.setTotalLessonCount(lessons.size());
        teacherScheduleStatResp.setLessonDates(lessons.stream().map(Lesson::getLessonDate).map(String::valueOf).toList());
        int completedLessonCount = lessons.stream().filter(lesson -> lesson.getLessonStatus().equals(LessonStatusEnum.COMPLETED)).toList().size();
        teacherScheduleStatResp.setCompletedLessonCount(completedLessonCount);
        return teacherScheduleStatResp;
    }

}
